Base Load

Package

In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pickle

Plot setting

In [2]:
plt.style.use("ggplot")
plt.rcParams['font.family'] = 'Noto Sans CJK TC'
plt.rcParams['axes.unicode_minus']=False

#custom_params = {"axes.spines.right": False, "axes.spines.top": False}
#sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

Data

Data for weather

  • temp : 溫度
  • RH : 濕度
  • WS : 風速
  • Precp : 降水量
In [3]:
# load 從資料檔案中讀取資料,並轉換為python的資料結構
with open("C:/Users/Rou_yi/OneDrive/上課/統計諮詢/統諮期末報告/data/stop_weather_data.pkl", 'rb+') as f:
     weather_data = pickle.load(f)
weather_data = pd.DataFrame(weather_data)
weather_data = weather_data.replace({"X":0, "...":np.nan, "&":0})
for col in ['year', 'month', 'day', 'temp', 'RH', 'WS', 'Precp']:
    weather_data[col] = pd.to_numeric(weather_data[col])
In [4]:
weather_data.columns
Out[4]:
Index(['year', 'month', 'day', 'StationName', 'Address', '觀測站點', 'temp', 'RH',
       'WS', 'Precp'],
      dtype='object')
In [5]:
weather_data.head()
Out[5]:
year month day StationName Address 觀測站點 temp RH WS Precp
0 2020 11 1 都會公園-文賢路站 奇美博物館文賢路停車場 仁德 25.2 77.0 1.4 0.0
1 2020 11 2 都會公園-文賢路站 奇美博物館文賢路停車場 仁德 25.4 80.0 2.0 0.0
2 2020 11 3 都會公園-文賢路站 奇美博物館文賢路停車場 仁德 24.6 69.0 2.8 0.0
3 2020 11 4 都會公園-文賢路站 奇美博物館文賢路停車場 仁德 24.5 62.0 1.8 0.0
4 2020 11 5 都會公園-文賢路站 奇美博物館文賢路停車場 仁德 25.0 68.0 1.9 0.0
In [6]:
weather_data_2 = weather_data[['year', 'month', '觀測站點', 'temp', 'RH', 'WS', 'Precp']].groupby(by=['year', 'month']).mean()
weather_data_2 = weather_data_2.reset_index()
weather_data_2.head()
Out[6]:
year month temp RH WS Precp
0 2020 11 24.170000 79.861039 1.981818 1.390476
1 2020 12 20.140344 78.908672 2.806200 0.256179
2 2021 1 16.554797 57.895685 2.415584 0.077503
3 2021 2 19.579453 54.628015 1.775649 0.519712
4 2021 3 22.295350 55.434855 2.057269 0.148303

Data for T-bike

In [7]:
data = pd.read_excel("C:/Users/Rou_yi/OneDrive/上課/統計諮詢/統諮期末報告/data/各站每月使用次數.xlsx")

data['total'] = data[['1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日', '9日',
       '10日', '11日', '12日', '13日', '14日', '15日', '16日', '17日', '18日', '19日',
       '20日', '21日', '22日', '23日', '24日', '25日', '26日', '27日', '28日', '29日',
       '30日', '31日']].sum(axis=1)
In [8]:
data.head()
Out[8]:
編號 站場 1日 2日 3日 4日 5日 6日 ... 23日 24日 25日 26日 27日 28日 29日 30日 31日 total
0 2021 11 1 保安轉運站 69 53 43 53 65 61 ... 55 68 56 69 89 112 91 58 0 2232
1 2021 11 2 十鼓文化村站 14 5 2 2 8 15 ... 9 6 7 4 47 10 12 8 0 316
2 2021 11 3 都會公園-文賢路站 9 11 0 6 8 18 ... 6 2 8 20 44 24 10 3 0 460
3 2021 11 4 都會公園-文華路站 1 17 8 4 8 30 ... 6 5 10 11 56 35 9 0 0 498
4 2021 11 5 都會公園-台1南停車場站 1 0 0 9 3 10 ... 3 0 0 0 18 0 0 0 0 89

5 rows × 36 columns

In [9]:
data.columns
Out[9]:
Index(['年', '月', '編號', '站場', '1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日',
       '9日', '10日', '11日', '12日', '13日', '14日', '15日', '16日', '17日', '18日',
       '19日', '20日', '21日', '22日', '23日', '24日', '25日', '26日', '27日', '28日',
       '29日', '30日', '31日', 'total'],
      dtype='object')
In [10]:
data_2 = data.drop(columns=['編號']).groupby(by=['年', '月']).mean()
data_2 = data_2.reset_index()
data_2.head()
Out[10]:
1日 2日 3日 4日 5日 6日 7日 8日 ... 23日 24日 25日 26日 27日 28日 29日 30日 31日 total
0 2019 11 41.639344 46.590164 46.622951 31.016393 30.639344 33.983607 35.163934 44.131148 ... 47.770492 48.704918 33.950820 32.081967 33.918033 34.098361 41.704918 50.081967 0.000000 1179.786885
1 2019 12 48.508197 30.721311 29.049180 19.508197 15.459016 13.655738 40.819672 45.065574 ... 33.065574 34.803279 39.196721 37.803279 43.065574 54.245902 21.688525 34.049180 43.311475 1128.180328
2 2020 1 55.836066 36.540984 39.540984 44.459016 44.065574 32.213115 33.475410 35.180328 ... 33.442623 20.967213 36.819672 25.868852 45.000000 24.672131 31.786885 30.983607 37.868852 1160.327869
3 2020 2 31.461538 32.815385 29.092308 27.800000 27.030769 29.215385 32.692308 31.292308 ... 32.107692 23.676923 21.738462 22.523077 27.615385 38.538462 42.861538 0.000000 0.000000 848.046154
4 2020 3 33.400000 22.285714 21.628571 20.414286 20.600000 28.171429 33.842857 34.771429 ... 19.614286 19.014286 19.142857 19.885714 24.100000 23.971429 29.057143 19.828571 21.028571 735.628571

5 rows × 34 columns

Plot

以整年為單位觀察【平均溫度、濕度、風速、降水量】

In [69]:
color = [sns.color_palette("tab10")[1], 
         sns.color_palette("tab10")[9],
         sns.color_palette("tab10")[2],
         sns.color_palette("tab10")[0]]
In [80]:
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

df_1 = weather_data_2[weather_data_2['year']==2021]
df_2 = data_2[data_2['年']==2021]

name = ['溫度', '濕度', '風速', '降水量']
fig, axes = plt.subplots(2, 2, figsize=(15, 8))
for i, item in enumerate(['temp', 'RH', 'WS', 'Precp']):
    ax1 = axes[i//2][i%2]  
    color_1 = 'black'
    sns.lineplot(data=df_2, x='月', y='total', color=color_1, ax=ax1)
    ax1.set_ylabel('使用量', color = color_1) 
    ax1.tick_params(axis ='y', labelcolor = color_1) 

    ax2 = ax1.twinx() 
    ax2.set_ylabel(name[i], color = color[i]) 
    ax2.tick_params(axis ='y', labelcolor = color[i]) 
    sns.lineplot(data=df_1, x='month', y=item, color=color[i], ax=ax2)
    
    plt.title('2021年使用量與'+name[i]+'關係圖')
    fig.tight_layout()

全部站點

全部站點 vs. 溫度

In [53]:
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))

st_list = data['站場'].unique()
for i, st in enumerate(st_list):
    # 定位第幾子圖
    ax1 = axes[i//4][i%4]  
    
    # 擷取需要資料
    df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
    df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
    df_1 = df_1[df_1['year']==2021]
    df_2 = data[(data['站場']==st) & (data['年']==2021)]

    # 開始繪圖
    color_1 = sns.color_palette("tab10")[1]
    sns.lineplot(data=df_1, x='month', y='temp', color=color_1, ax=ax1)
    ax1.set_ylabel('溫度', color = color_1) 
    ax1.tick_params(axis ='y', labelcolor = color_1) 
    ax2 = ax1.twinx() 
    
    color_2 = 'black'
    sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
    ax2.set_ylabel('使用量', color = color_2) 
    ax2.tick_params(axis ='y', labelcolor = color_2) 

    plt.title('{}. 2021年 - {}'.format(i+1, st))
    fig.tight_layout()

全部站點 vs. 濕度

In [52]:
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))

st_list = data['站場'].unique()
for i, st in enumerate(st_list):
    # 定位第幾子圖
    ax1 = axes[i//4][i%4]  
    
    # 擷取需要資料
    df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
    df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
    df_1 = df_1[df_1['year']==2021]
    df_2 = data[(data['站場']==st) & (data['年']==2021)]

    # 開始繪圖
    color_1 = sns.color_palette("tab10")[9]
    sns.lineplot(data=df_1, x='month', y='RH', color=color_1, ax=ax1)
    ax1.set_ylabel('濕度', color = color_1) 
    ax1.tick_params(axis ='y', labelcolor = color_1) 
    ax2 = ax1.twinx() 
    
    color_2 = 'black'
    sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
    ax2.set_ylabel('使用量', color = color_2) 
    ax2.tick_params(axis ='y', labelcolor = color_2) 

    plt.title('{}. 2021年 - {}'.format(i+1, st))
    fig.tight_layout()

全部站點 vs. 風速

In [51]:
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))

st_list = data['站場'].unique()
for i, st in enumerate(st_list):
    # 定位第幾子圖
    ax1 = axes[i//4][i%4]  
    
    # 擷取需要資料
    df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
    df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
    df_1 = df_1[df_1['year']==2021]
    df_2 = data[(data['站場']==st) & (data['年']==2021)]

    # 開始繪圖
    color_1 = sns.color_palette("tab10")[2]
    sns.lineplot(data=df_1, x='month', y='WS', color=color_1, ax=ax1)
    ax1.set_ylabel('風速', color = color_1) 
    ax1.tick_params(axis ='y', labelcolor = color_1) 
    ax2 = ax1.twinx() 
    
    color_2 = 'black'
    sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
    ax2.set_ylabel('使用量', color = color_2) 
    ax2.tick_params(axis ='y', labelcolor = color_2) 

    plt.title('{}. 2021年 - {}'.format(i+1, st))
    fig.tight_layout()

全部站點 vs. 降水量

In [50]:
# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

# 多子圖設置
fig, axes = plt.subplots(20, 4, figsize=(24, 80))

st_list = data['站場'].unique()
for i, st in enumerate(st_list):
    # 定位第幾子圖
    ax1 = axes[i//4][i%4]  
    
    # 擷取需要資料
    df_1 = weather_data[weather_data['StationName']==st].drop(columns=['StationName', 'Address', '觀測站點', 'day'])
    df_1 = df_1.groupby(by=['year', 'month']).mean().reset_index()
    df_1 = df_1[df_1['year']==2021]
    df_2 = data[(data['站場']==st) & (data['年']==2021)]

    # 開始繪圖
    color_1 = sns.color_palette("tab10")[0]
    sns.lineplot(data=df_1, x='month', y='Precp', color=color_1, ax=ax1)
    ax1.set_ylabel('降水量', color = color_1) 
    ax1.tick_params(axis ='y', labelcolor = color_1) 
    ax2 = ax1.twinx() 
    
    color_2 = 'black'
    sns.lineplot(data=df_2, x='月', y='total', color = color_2, ax=ax2)
    ax2.set_ylabel('使用量', color = color_2) 
    ax2.tick_params(axis ='y', labelcolor = color_2) 

    plt.title('{}. 2021年 - {}'.format(i+1, st))
    fig.tight_layout()

研究 2021 / 6 的使用量低潮

以月分的角度觀看各站點的使用量,可以發現今年 6 月台南下了很多雨,影響到該月的使用量為大多數站點的使用量。
以下將 6 月的 30 天使用量和降雨量進行繪圖分析,可以看到確實在大多數有下雨的日子,使用量通常會極低,但有某些站點較不呈現此關係,如:

  1. 保安轉運站
  2. 台南火車站前站
  3. 陽明交通大學台南校區
  4. 新營火車站
  5. 台南二中站
  6. 新興國小站
  7. 東寧運動公園站 (成大旁)

而以上地點大多位於「學校」、「火車站」附近。

In [49]:
day_col = ['1日', '2日', '3日', '4日', '5日', '6日', '7日', '8日', '9日', '10日', 
           '11日', '12日', '13日', '14日', '15日', '16日', '17日', '18日', '19日', '20日', 
           '21日', '22日', '23日', '24日', '25日', '26日', '27日', '28日', '29日', '30日', '31日']

# 繪圖設置
custom_params = {"axes.spines.top": False}
sns.set_theme(font='Noto Sans CJK TC', palette=sns.color_palette(), style="ticks", rc=custom_params)

# 多子圖設置
fig, axes = plt.subplots(40, 2, figsize=(20, 160))

st_list = data['站場'].unique()
for i, st in enumerate(st_list):
    # 定位第幾子圖
    ax1 = axes[i//2][i%2]  
    
    # 擷取需要資料
    df_1 = weather_data[(weather_data['year']==2021) & (weather_data['month']==6) & (weather_data['StationName']==st)]
    df_2 = data[(data['年']==2021) & (data['月']==6) & (data['站場']==st)][day_col].transpose().reset_index()
    if df_2.shape[1] < 2:
        df_2['value'] = [0]*df_2.shape[0]
    df_2.columns = ['day', 'value']
    df_2['day'] = list(range(1, df_2.shape[0]+1))

    # 開始繪圖
    color_1 = sns.color_palette("tab10")[0]
    sns.lineplot(data=df_1, x='day', y='Precp', color=color_1, ax=ax1)
    ax1.set_ylabel('降水量', color = color_1) 
    ax1.tick_params(axis ='y', labelcolor = color_1) 
    ax2 = ax1.twinx() 
    
    color_2 = 'black'
    sns.lineplot(data=df_2, x='day', y='value', color = color_2, ax=ax2)
    ax2.set_ylabel('使用量', color = color_2) 
    ax2.tick_params(axis ='y', labelcolor = color_2) 

    plt.title('{}. 2021年6月 - {}'.format(i+1, st))
    fig.tight_layout()

其他

給大家愛心 🧡💛💚💙💜